<?php

class InformationManageController extends Controller
{

    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules()
    {
        return array(

            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('add','publish','search','getlist','getbyid','delete'),
                'users'=>array('admin'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
		);
	}

    public function actionAdd()
    {
        $info = new Information();
        $info->name = $_POST["name"];
        $info->subname = $_POST["subname"];
        $info->content = $_POST["param"];
        $info->type = $_POST["type"];
        $info->thumb = $_POST["thumb"];
        $info->updatetime = time();

        $info->save(false);

        $file = fopen("/var/www/tao/tao/info/{$info->_id}.html", "x");
        fwrite($file,'<html>
                    <head>
                        <meta content="text/html; charset=utf8" http-equiv="Content-Type"/>
                        <meta name="viewport" content="width=device-width, initial-scale=1.0">
                        <title>'.$info->name.'</title>
                        <link rel="stylesheet" href="http://cdn.bootcss.com/twitter-bootstrap/3.0.3/css/bootstrap.min.css">
                        <style type="text/css">
                        p {
                            text-indent: 2em;
                        }
                        </style>
                    </head>
                    <body>
                    <div class="container">');
        fwrite($file, "<h1>{$info->name}</h1>");
        fwrite($file, "<h6>{$info->subname}</h6>");
        foreach ($info->content as $key => $value) {
            if(strpos($value, "files/")===0)
                fwrite($file, "<img class='img-thumbnail' src='http://115.29.161.76/dubai/{$value}'></img>");
            else
                fwrite($file, "<p>{$value}</p>");
        }
        fwrite($file, "</div><body></html>");    
        fclose($file);
    }

    /**
     * 获得版本信息
     */
    public function actionGetList($iDisplayStart=0,$iDisplayLength=10,$search_name='')
    {
        $resultColumn = array('name','type');
        $query = array(
            'conditions' => array(
               // 'channel'=>array('=='=>constant("channel_homepage"))
            ),
            'select'=>$resultColumn,
            'limit'=>$iDisplayLength,
            'offset'=>$iDisplayStart,
        );

        $result = Information::model()->findAll($query);

      
        $records = array();
        $records["aaData"] = array(); 

        if(!empty($result))
            foreach ($result as $key => $value) {
            $tmp = array();
            $value->_id = (string)$value->_id;
            foreach ($value as $key1=>$value1) {
                if(in_array($key1, $resultColumn))
                {
                   array_push($tmp, $value1);
                }
            }
            array_push($tmp, '<a href="javascript:;" data-id="'.$value->_id.'" class="btn btn-xs red btn-removable"><i class="fa fa-times"></i> Delete</a> <a href="info/'.$value->_id.'.html" class="btn btn-xs green"><i class="fa fa-times"></i> view</a>  ');
            array_push($records["aaData"], $tmp);
        }

        $records["iTotalRecords"] = Information::model()->count($query);
        $records["iTotalDisplayRecords"] = Information::model()->count($query);
  
        echo json_encode($records);
    }

    public function actionSearch($name)
    {
        $resultColumn = array('name_en','_id');
        $query = array(
            'conditions' => array(
                'status' => array('>'=>0),
                'name_en'=>array('=='=>new MongoRegex("/^{$name}/i"))
            ),
            'select'=>$resultColumn,
        );

        $result = Brand::model()->findAll($query);
        $records = array();
        if(!empty($result))
        {
            foreach ($result as $key => $value) {
                $value->_id = (string)$value->_id;
                foreach ($value as $key1=>$value1) {
                    if(!in_array($key1, $resultColumn))
                    {
                        unset($value[$key1]);
                    }
                }

                $obj['id'] = $value->_id;
                $obj['text'] = $value->name_en;
                array_push($records, $obj);
            }
        }



        echoStringToMobile(json_encode(array("results"=>$records)));

    }

    public function actionGetById($id)
    {
        $brand = Brand::model()->findByPk(new MongoId($id));
        $brand->_id = (string)$brand->_id;
        echoStringToMobile(json_encode($brand));
    }

    public function actionDelete($id)
    {
        Information::model()->deleteByPk(new MongoId($id));
    }
}